<template>
  <div class="commonBoxmp">
    <TableV2
      class="flex1"
      :apiFunction="listType"
      ref="tableV2Ref"
      :columns="state.columnData"
    >
      <template #tableHeader>
        <div class="jcb aic w100">
          <el-button type="primary" @click="handleAdd">添加字典</el-button>
        </div>
      </template>
      <template #operation>
        <el-table-column label="操作" fixed="right" width="160px" align="left">
          <template #default="scope">
            <el-link type="primary" @click="handleUpdate(scope.row)"
              >修改</el-link
            >
            <el-link class="mx5"  type="primary" @click="handleDelete(scope.row)"
              >删除</el-link
            >
          </template>
        </el-table-column>
      </template>
    </TableV2>

    <el-dialog
      v-model="state.userVisible"
      :title="state.title"
      width="50%"
      destroy-on-close
      center
    >
      <el-form
        ref="ruleFormRef"
        :model="state.form"
        :rules="state.rules"
        label-width="120px"
        class="demo-ruleForm"
      >
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="字典名称" prop="dictName">
              <el-input
                v-model="state.form.dictName"
                placeholder="请输入字典名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="字典类型" prop="dictType">
              <el-input
                v-model="state.form.dictType"
                placeholder="请输入字典类型"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="状态">
              <selectDict
                class="w100"
                dictCode="sys_normal_disable"
                v-model="state.form.status"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="state.form.remark"
                type="textarea"
                placeholder="请输入内容"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="state.userVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm(ruleFormRef)">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="tsx">
import { ref, reactive, getCurrentInstance, onMounted } from "vue";
import { useRouter, useRoute } from "vue-router";
import Column from "/@ts/components/table/Column";
import selectDict from "/@/components/selectDict/index.vue";
import DictTag from "/@/components/DictTag/index.vue";
import { ElMessage, ElMessageBox, FormInstance } from "element-plus";
import {
  listType,
  getType,
  delType,
  addType,
  updateType,
} from "/@ts/axios/urlApi/system/dict/type";
import formatTool from "/@ts/hooks/format";
import { routerPush } from "/@ts/hooks/route";
const router = useRouter();
const route = useRoute();
const { parseTime, addDateRange, getDict } = formatTool();

const state: any = reactive({
  // 表格基础配置 start
  form: {},
  title: "",
  rules: {
    dictName: [
      { required: true, message: "字典名称不能为空", trigger: "blur" },
    ],
    dictType: [
      { required: true, message: "字典类型不能为空", trigger: "blur" },
    ],
  },
  dateRange: [], //时间范围筛选

  height: 0,
  tableData: [],
  columnData: [
    {
      label: "字典编号",
      visible: true,
      prop: "dictId",
    },
    {
      label: "字典名称",
      prop: "dictName",
       renderHeader: ({ column }: any) => {
        return (
          <filterBox
            title={column.label}
            valueKey={column.property}
            flterType="input"
            onChange={tableV2Ref.value.handleFilterBox}
            data={tableV2Ref.value.formFilter}
          ></filterBox>
        );
      },
      visible: true,
    },
    {
      label: "字典类型",
      visible: true,
      prop: "dictType",
       renderHeader: ({ column }: any) => {
        return (
          <filterBox
            title={column.label}
            valueKey={column.property}
            flterType="input"
            onChange={tableV2Ref.value.handleFilterBox}
            data={tableV2Ref.value.formFilter}
          ></filterBox>
        );
      },
      render: ({ row }: any) => {
        return (
          <router-link
            to={"/system/dict-data/index/" + row.dictId}
            className="link-type"
          >
            <span>{row.dictType}</span>
          </router-link>
        );
      },
    },
    {
      label: "状态",
      visible: true,
      prop: "status",
      render: ({ row }: any) => {
        return (
          <DictTag
            values={row.status}
            dataType="primary"
            options={dict.sys_normal_disable}
          ></DictTag>
        );
      },
    },

    {
      label: "创建时间",
      visible: true,
      prop: "createTime",
    },
  ],
  loading: false,
  // 表格基础配置 end
});
// 表格基础配置 start
 
const getCurrentColumns = (data: Column[]) => {
  state.columnData = data;
};


const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate();
  if (state.form.dictId != undefined) {
    updateType(state.form).then((response: ResponseConfig) => {
      ElMessage.success("修改成功");
      state.userVisible = false;
      getList();
    });
  } else {
    addType(state.form).then((response: ResponseConfig) => {
      ElMessage.success("新增成功");
      state.userVisible = false;
      getList();
    });
  }
};
const handleDelete = (row: any) => {
  const dictId = row.dictId;
  ElMessageBox.confirm('是否确认删除编号为"' + dictId + '"的数据项？')
    .then(function () {
      return delType(dictId);
    })
    .then(() => {
      ElMessage.success("删除成功");
      getList();
    })
    .catch(function () {});
};
/** 修改按钮操作 */
const handleUpdate = (row: any) => {
  const dictId = row.dictId;
  getType(dictId).then((response: ResponseConfig) => {
    state.title = "修改字典";
    state.form = response.data;
    state.userVisible = true;
  });
};
const handleAdd = () => {
  state.form = {};
  state.title = "添加字典";
  state.userVisible = true;
};
// 表格基础配置 end
const tableV2Ref = ref();
//查询用户列表
const getList = () => {
  tableV2Ref.value.getList()
};
const handleDetail = (dictId: any) => {
  routerPush({
    path: "/system/dict-data/index/" + dictId,
  });
};
let dict: any = reactive({
  sys_normal_disable: [],
});
const ruleFormRef = ref<FormInstance>();
onMounted(async () => {
  dict.sys_normal_disable = await getDict("sys_normal_disable");
  getList();
});
</script>

<style scoped lang="scss"></style>
